package com.luckyframe.project.jmeter.jmeterTestPlanMetric.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.luckyframe.common.utils.StringUtils;
import com.luckyframe.common.utils.poi.ExcelUtil;
import com.luckyframe.framework.aspectj.lang.annotation.Log;
import com.luckyframe.framework.aspectj.lang.enums.BusinessType;
import com.luckyframe.framework.web.controller.BaseController;
import com.luckyframe.framework.web.domain.AjaxResult;
import com.luckyframe.framework.web.page.TableDataInfo;
import com.luckyframe.project.jmeter.consumer.SampleHandler;
import com.luckyframe.project.jmeter.jmeterTestPlan.service.IJmeterTestPlanService;
import com.luckyframe.project.jmeter.jmeterTestPlanMetric.domain.JmeterTestPlanMetric;
import com.luckyframe.project.jmeter.jmeterTestPlanMetric.enums.PlanMetricStatusEnum;
import com.luckyframe.project.jmeter.jmeterTestPlanMetric.service.IJmeterTestPlanMetricService;
import com.luckyframe.project.jmeter.report.domain.Chart;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标监控 信息操作处理
 * 
 * @author lancelot
 * @date 2020-11-09
 */
@Controller
@RequestMapping("/jmeter/jmeterTestPlanMetric")
public class JmeterTestPlanMetricController extends BaseController
{

	private static final Logger logger = LoggerFactory.getLogger(JmeterTestPlanMetricController.class);


	private String prefix = "jmeter/jmeterTestPlanMetric";

	private String graph = "jmeter/planGraph";

	
	@Autowired
	private IJmeterTestPlanMetricService jmeterTestPlanMetricService;


	@Autowired
	private IJmeterTestPlanService jmeterTestPlanService;

	private SampleHandler sampleHandler=new SampleHandler();

	
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:view")
	@GetMapping()
	public String jmeterTestPlanMetric()
	{
	    return prefix + "/jmeterTestPlanMetric";
	}


	@GetMapping("displayGraph")
	public String displayGraph(@RequestParam("id") Long id, ModelMap mmap)
	{

		JmeterTestPlanMetric metric = jmeterTestPlanMetricService.selectJmeterTestPlanMetricById(id);
		if(metric==null){
			throw new RuntimeException("未找到记录");
		}

		boolean executing = PlanMetricStatusEnum.isExecuting(metric.getStatus());

		mmap.put("executing",executing );
		mmap.put("id",id );
		return graph + "/planGraph";
	}




	/**
	 * 查询指标监控列表
	 */
	@GetMapping("/getGraphData")
	@ResponseBody
	public Object getGraphData(@RequestParam("id") Long id)
	{
		Map<String,Object> result=new HashMap<>();

		JmeterTestPlanMetric jmeterTestPlanMetric = jmeterTestPlanMetricService.selectJmeterTestPlanMetricById(id);
		if(jmeterTestPlanMetric==null){
			result.put("isRunning",false);
			return result;
		}

		String resultData = jmeterTestPlanMetric.getResultData();
		if(StringUtils.isEmpty(resultData)){
			resultData="[]";
		}

		List<Chart> charts = JSON.parseArray(resultData, Chart.class);
		result.put("charts", Maps.uniqueIndex(charts, Chart::getName));
		result.put("isRunning", PlanMetricStatusEnum.isExecuting(jmeterTestPlanMetric.getStatus()));

		return result;
	}

	/**
	 * 查询指标监控列表
	 */
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(JmeterTestPlanMetric jmeterTestPlanMetric)
	{
		startPage();
        List<JmeterTestPlanMetric> list = jmeterTestPlanMetricService.selectJmeterTestPlanMetricList(jmeterTestPlanMetric);
		return getDataTable(list);
	}



	/**
	 * 查询指标监控列表
	 */
	@RequestMapping("/listByIds")
	@ResponseBody
	public List<JmeterTestPlanMetric> listByIds(@RequestBody List<Long> ids)
	{
		return jmeterTestPlanMetricService.listByIds(ids);

	}



	/**
	 * 导出指标监控列表
	 */
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JmeterTestPlanMetric jmeterTestPlanMetric)
    {
    	List<JmeterTestPlanMetric> list = jmeterTestPlanMetricService.selectJmeterTestPlanMetricList(jmeterTestPlanMetric);
        ExcelUtil<JmeterTestPlanMetric> util = new ExcelUtil<JmeterTestPlanMetric>(JmeterTestPlanMetric.class);
        return util.exportExcel(list, "jmeterTestPlanMetric");
    }
	
	/**
	 * 新增指标监控
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存指标监控
	 */
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:add")
	@Log(title = "指标监控", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(JmeterTestPlanMetric jmeterTestPlanMetric)
	{		
		return toAjax(jmeterTestPlanMetricService.insertJmeterTestPlanMetric(jmeterTestPlanMetric));
	}

	/**
	 * 修改指标监控
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		JmeterTestPlanMetric jmeterTestPlanMetric = jmeterTestPlanMetricService.selectJmeterTestPlanMetricById(id);
		mmap.put("jmeterTestPlanMetric", jmeterTestPlanMetric);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存指标监控
	 */
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:edit")
	@Log(title = "指标监控", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(JmeterTestPlanMetric jmeterTestPlanMetric)
	{		
		return toAjax(jmeterTestPlanMetricService.updateJmeterTestPlanMetric(jmeterTestPlanMetric));
	}
	
	/**
	 * 删除指标监控
	 */
	@RequiresPermissions("jmeter:jmeterTestPlanMetric:remove")
	@Log(title = "指标监控", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(jmeterTestPlanMetricService.deleteJmeterTestPlanMetricByIds(ids));
	}
	
}
